package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior>
        implements ApUnlikesBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult Unlike(UnLikesBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getType().shortValue()<0
        ||dto.getType().shortValue()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),
                dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUnlikesBehavior apUnlikesBehavior =
                getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId()));

     /*   if (apUnlikesBehavior == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"暂无不喜欢");
        }*/

        if (apUnlikesBehavior != null && dto.getType().intValue() == apUnlikesBehavior.getType().intValue()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,
                    "该文章已添加至不喜欢，无需继续操作");
        }
            //保存不喜欢操作
        if (apUnlikesBehavior==null&&dto.getType().equals(ApUnlikesBehavior.Type.UNLIKE.getCode())){
            apUnlikesBehavior=new ApUnlikesBehavior();
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setType(dto.getType());
            save(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            apUnlikesBehavior.setType(dto.getType());
            updateById(apUnlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

    }
}
